﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using KinectGame;

namespace MemoGame
{
    public partial class GameUI : KinectBaseForm
    {
        private SortedDictionary<string, GameButton> buttons;
        private IList<string> alphabet;
        private bool kinectMode;
        private int size; 

        public SortedDictionary<string, GameButton> Buttons
        {
            get { return buttons; }
            set { buttons = value; }
        }

        public GameUI(int size)
        {
            this.size = size;
            this.kinectMode = false; 
            InitializeComponent(); 
        }

        public override void SetGameContext(GameContext gameContext)
        {
            base.SetGameContext(gameContext);
            Game.getInstance().GameContext = this.gameContext; 
        }

        public GameUI()
        {
            // TODO: Complete member initialization
        }

        public override void SetupForKinectMode()
        {
            base.SetupForKinectMode();
            this.btnMenu.Visible = false;
            this.lblMenu.Visible = true;
            this.alphabet = new List<string>();
            CreateAlphabet();
            this.kinectMode = true; 
        }

        private void CreateAlphabet()
        {
            this.alphabet.Add("A");
            this.alphabet.Add("B");
            this.alphabet.Add("C");
            this.alphabet.Add("D");
            this.alphabet.Add("E");
            this.alphabet.Add("F");
            this.alphabet.Add("G");
            this.alphabet.Add("H");
            this.alphabet.Add("I");
            this.alphabet.Add("J");
            this.alphabet.Add("K");
            this.alphabet.Add("L");
            this.alphabet.Add("M");
            this.alphabet.Add("N");
            this.alphabet.Add("O");
            this.alphabet.Add("P");
            this.alphabet.Add("R");
            this.alphabet.Add("S");
            this.alphabet.Add("T");
            this.alphabet.Add("U");
            this.alphabet.Add("V");
            this.alphabet.Add("W");
            this.alphabet.Add("X");
            this.alphabet.Add("Y");
            this.alphabet.Add("Z");
        }

        public override void ApplicationSpecificAction()
        {
            base.ApplicationSpecificAction();
            OpenMenu(); 
        }

        public override void highlightButton(int horizontalIndex, int verticalIndex)
        {
            int horizontalIndexForDictionary = horizontalIndex + 1;
            this.Buttons[this.alphabet[verticalIndex] + horizontalIndexForDictionary].Highlight();
            if (verticalIndex > 0)
            {
                this.Buttons[this.alphabet[verticalIndex-1] + horizontalIndexForDictionary].Unhighlight();
            }
            if (verticalIndex < Game.getInstance().GameSize - 1)
            {
                this.Buttons[this.alphabet[verticalIndex + 1] + horizontalIndexForDictionary].Unhighlight();
            }
            if (horizontalIndex > 0)
            {
                this.Buttons[this.alphabet[verticalIndex] + horizontalIndex].Unhighlight();
            }
            if (horizontalIndex < Game.getInstance().GameSize - 1)
            {
                horizontalIndexForDictionary++; 
                this.Buttons[this.alphabet[verticalIndex] + horizontalIndexForDictionary].Unhighlight();
            }
            Console.WriteLine(verticalIndex + " " + horizontalIndex + " " + this.alphabet[verticalIndex] + horizontalIndexForDictionary); 
        }

        public override void Click(int horizontalIndex, int verticalIndex)
        {
            horizontalIndex++;
            Game.getInstance().inspectCard(this.Buttons[this.alphabet[verticalIndex] + horizontalIndex]); 
        }

        public override int SetHorizontalItemNumber()
        {
            return Game.getInstance().GameSize;
        }

        public override int SetVerticalItemNumber()
        {
            return Game.getInstance().GameSize;
        }

        private void button_Click(object sender, EventArgs e)
        {
            GameButton b = (GameButton)sender;
            Game game = Game.getInstance();

            game.inspectCard(b);
        }

        public void refreshPanel()
        {
            this.panel1.Refresh();
        }

        public void updateScore(String newScore)
        {
            this.scoreLabel.Text = newScore;
        }

        public void updatePlayer(String newPlayer)
        {
            playerLabel.Text = newPlayer;
        }

        private void GameUI_KeyDown(object sender, KeyEventArgs e)
        {
            Console.WriteLine("Key: " + e.KeyCode);
            if (e.KeyCode == Keys.Escape)
            {
                DialogResult dr = MessageBox.Show("It's so obvious, that your memory sucks. Just admit it and quit, it's what you always do anyway",
                      "Sanity Check", MessageBoxButtons.OKCancel);
                switch (dr)
                {
                    case DialogResult.OK:
                        {
                            Application.Exit();
                        } break;
                    case DialogResult.Cancel:
                        {
                            e.Handled = true;
                        } break;
                }
            }
            else if (e.KeyCode == Keys.Q)
            {
                Application.Exit();
            }
        }

        private void btnMenu_Click(object sender, EventArgs e)
        {
            OpenMenu(); 
        }

        private void OpenMenu()
        {
            Game.getInstance().stopTimer();
            this.gameContext.ApplicationMode(this, new MenuUI(), 0); 
        }

        private void GameUI_Load(object sender, EventArgs e)
        {
            Game game = Game.getInstance();
            //because buttons are created dynamically, we need to store them somewhere, so we could use them
            buttons = new SortedDictionary<string, GameButton>();

            //initialize variables for dynamic drawing
            int columns = 1;
            int rows = 1;
            int betweenX = (int)((100 * (this.panel1.Size.Width - 20)) / (size * 720));
            int cardX = (int)((620 * (this.panel1.Size.Width - 20)) / (size * 720));
            int betweenY = (int)((80 * (this.panel1.Size.Height - 20)) / (size * 460));
            int cardY = (int)((360 * (this.panel1.Size.Height - 20)) / (size * 460));

            foreach (var g in game.Board.Cards)
            {
                g.Value.Image = new Bitmap(Image.FromFile(g.Value.Filename), cardX, cardY);
                GameButton button = new GameButton();
                button.BackColor = System.Drawing.SystemColors.MenuBar;

                button.Location = new Point(
                    (int)(20 + cardX * (columns - 1) + (columns - 1) * betweenX),
                    (int)(20 + cardY * (rows - 1) + (rows - 1) * betweenY)
                    );
                button.Name = g.Key;
                button.Size = new System.Drawing.Size(cardX, cardY);
                button.TabIndex = columns * rows;
                button.Text = g.Key;
                button.Font = new Font("Microsoft Sans Serif", 20, FontStyle.Bold, GraphicsUnit.Point);
                button.UseVisualStyleBackColor = true;
                if (!this.kinectMode) button.Click += button_Click;
                //button.TextChanged += textChanged;
                this.panel1.Controls.Add(button);
                button.Visible = true;
                Console.WriteLine(button.Text + " " + button.Size.Width + " " + button.Size.Height + " "
                    + button.Location.X + " " + button.Location.Y);
                buttons.Add(g.Key, button);

                if (columns < size)
                {
                    columns++;
                }
                else
                {
                    columns = 1;
                    rows++;
                }
            }
        }
    }
}
